package com.cms.model.vo;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.*;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;

import com.cms.model.dto.PageDto;
import com.cms.utils.tools.DateUtil;

/**
* 描述:系统日志
* @author yangtao
* @date 2018/04/17
*/
@Entity
@Table(name="sys_log")
public class SysLog extends PageDto implements Serializable{
   
	private static final long serialVersionUID = 1L;
	
    @Id
    @Column(name = "log_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer  logId;
    
    /**日志标题*/
    @Column(name = "log_title")
    private String  logTitle;
    
    /**日志类型 info error*/
    @Column(name = "log_type")
    private String  logType;
    
    /**日志请求url*/
    @Column(name = "log_url")
    private String  logUrl;
    
    /**请求方式 get post*/
    @Column(name = "log_method")
    private String  logMethod;
    
    /**请求参数*/
    @Column(name = "log_params")
    private String  logParams;
    
    /**请求异常*/
    @Column(name = "log_exception")
    private String  logException;
    
    /**请求用户Id*/
    @Column(name = "log_user_name")
    private String  logUserName;
    
    /**请求开始时间*/
    @Column(name = "log_start_time")
    private Date  logStartTime;
    
    /**请求耗时*/
    @Column(name = "log_elapsed_time")
    private Long  logElapsedTime;
    
    /**
     * 查询项
     */
    @Transient
    private String searchTerm;
    /**
     * 查询内容
     */
    @Transient
    private String searchContent;
    
    @Transient
    private String beginTime;
    

    public SysLog() {}

    public SysLog(String logTitle, String logType, String logUrl, String logMethod, String logUserName, Date logStartTime, Long logElapsedTime) {
        this.logTitle = logTitle;
        this.logType = logType;
        this.logUrl = logUrl;
        this.logMethod = logMethod;
        this.logUserName = logUserName;
        this.logStartTime = logStartTime;
        this.logElapsedTime = logElapsedTime;
    }

    public SysLog(String logTitle, String logType, String logUrl, String logMethod, String logException, String logUserName,Date logStartTime, Long logElapsedTime) {
        this.logTitle = logTitle;
        this.logType = logType;
        this.logUrl = logUrl;
        this.logMethod = logMethod;
        this.logException = logException;
        this.logUserName = logUserName;
        this.logStartTime = logStartTime;
        this.logElapsedTime = logElapsedTime;
    }

    
    public Integer getLogId() {
        return this.logId;
    }

    public void setLogId(Integer logId) {
        this.logId = logId;
    }
    
    public String getLogTitle() {
        return this.logTitle;
    }

    public void setLogTitle(String logTitle) {
        this.logTitle = logTitle;
    }
    
    public String getLogType() {
        return this.logType;
    }

    public void setLogType(String logType) {
        this.logType = logType;
    }
    
    public String getLogUrl() {
        return this.logUrl;
    }

    public void setLogUrl(String logUrl) {
        this.logUrl = logUrl;
    }
    
    public String getLogMethod() {
        return this.logMethod;
    }

    public void setLogMethod(String logMethod) {
        this.logMethod = logMethod;
    }
    
    public String getLogParams() {
        return this.logParams;
    }

    public void setLogParams(String logParams) {
        this.logParams = logParams;
    }
    
    public String getLogException() {
        return this.logException;
    }

    public void setLogException(String logException) {
        this.logException = logException;
    }
    
    public String getLogUserName() {
        return this.logUserName;
    }

    public void setLogUserName(String logUserName) {
        this.logUserName = logUserName;
    }
    
    public Date getLogStartTime() {
        return this.logStartTime;
    }

    public void setLogStartTime(Date logStartTime) {
        this.logStartTime = logStartTime;
    }
    
    public Long getLogElapsedTime() {
        return this.logElapsedTime;
    }

    public void setLogElapsedTime(Long logElapsedTime) {
        this.logElapsedTime = logElapsedTime;
    }
    
    public String getSearchTerm() {
		return searchTerm;
	}

	public void setSearchTerm(String searchTerm) {
		this.searchTerm = searchTerm;
	}

	public String getSearchContent() {
		return searchContent;
	}

	public void setSearchContent(String searchContent) {
		this.searchContent = searchContent;
	}

	public String getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}
	
	 /**
     * 设置请求参数
     * @param paramMap
     */
    public void setMapToParams(Map<String, String[]> paramMap) {
        if (paramMap == null) {
            return;
        }
        Map<String, Object> params = new HashMap<>();
        for (Map.Entry<String, String[]> param : paramMap.entrySet()) {
            String key = param.getKey();
            String paramValue = (param.getValue() != null && param.getValue().length > 0 ? param.getValue()[0] : "");
            String obj = StringUtils.endsWithIgnoreCase(param.getKey(), "password") ? "" : paramValue;
            params.put(key,obj);
        }
        this.logParams = Json.toJson(params, JsonFormat.compact());
    }
    
    /**
     * 开始日期格式化
     * @return
     */
    public String getLogStartTime_Lable(){
        if(this.getLogStartTime() != null){
            return DateUtil.Date2Stirng2Second(logStartTime);
        }
        return null;
    }
    
    /**
     * 日志类型
     * @return
     */
    public String getLogType_Lable(){
        if(null != this.getLogType()){
        	if(("info").equals(this.getLogType())){
                return "业务日志";
            }else if(("error").equals(this.getLogType())){
                return "异常日志";
            }	
        }
        return null;
    }


	@Override
    public String toString(){
    	return ToStringBuilder.reflectionToString(this);
    }
}